//
//  NSMutableArray+Lc.m
//  18Birdies
//
//  Created by Lc on 16/5/20.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "LcJson.h"
#import "LcPreconditions.h"
#import "NSMutableArray+Lc.h"

@implementation NSMutableArray (Lc)

+ (NSMutableArray*)arrayWithObjectsInArray:(NSArray*)array withRange:(NSRange)range {
  LC_CHECK_TRUE(range.location != NSNotFound);
  LC_CHECK_TRUE(range.location + range.length <= array.count);
  NSMutableArray* mutableArray = [NSMutableArray arrayWithCapacity:range.length];
  for (int i = 0; i < range.length; i++) {
    [mutableArray addObject:array[i + range.location]];
  }
  return mutableArray;
}

- (void)fixLength:(int)length defaultValue:(NSObject*)defaultValue {
  LC_CHECK_TRUE(length > 0);
  if (defaultValue == nil) {
    defaultValue = [NSNull null];
  }
  if (self.count < length) {
    int need = length - (int)self.count;
    for (int i = 0; i < need; ++i) {
      if (defaultValue == [NSNull null] || [defaultValue isKindOfClass:NSNumber.class]) {
        [self addObject:defaultValue];
      } else {
        [self addObject:[[LcJson sharedInstance] jsonCopy:defaultValue]];
      }
    }
  } else if (self.count > length) {
    int need = (int)self.count - length;
    for (int i = 0; i < need; ++i) {
      [self removeLastObject];
    }
  }
  if (defaultValue != [NSNull null]) {
    for (int i = 0; i < length; ++i) {
      if (self[i] == [NSNull null]) {
        if ([defaultValue isKindOfClass:NSNumber.class]) {
          self[i] = defaultValue;
        } else {
          self[i] = [[LcJson sharedInstance] jsonCopy:defaultValue];
        }
      }
    }
  }
}

+ (NSMutableArray*)fixArray:(NSMutableArray*)array
                     length:(int)length
               defaultValue:(NSObject*)defaultValue {
  if (array == nil) {
    array = [NSMutableArray arrayWithCapacity:length];
  }
  [array fixLength:length defaultValue:defaultValue];
  return array;
}

+ (NSMutableArray*)clearAndFixArray:(NSMutableArray*)array
                             length:(int)length
                       defaultValue:(NSObject*)defaultValue {
  if (array == nil) {
    array = [NSMutableArray arrayWithCapacity:length];
  }
  [array removeAllObjects];
  [array fixLength:length defaultValue:defaultValue];
  return array;
}

+ (NSMutableArray*)arrayWithLength:(int)length defaultValue:(NSObject*)defaultValue {
  return [NSMutableArray fixArray:nil length:length defaultValue:defaultValue];
}

+ (NSMutableArray*)arrayWithDimension1Length:(int)dimension1Length
                            dimension2Length:(int)dimension2Length
                                defaultValue:(NSObject*)defaultValue {
  return [self fixArray:nil
       dimension1Length:dimension1Length
       dimension2Length:dimension2Length
           defaultValue:defaultValue];
}

+ (NSMutableArray*)fixArray:(NSMutableArray*)array
           dimension1Length:(int)dimension1Length
           dimension2Length:(int)dimension2Length
               defaultValue:(NSObject*)defaultValue {
  array = [self fixArray:array length:dimension1Length defaultValue:[NSNull null]];
  for (int i = 0; i < dimension1Length; ++i) {
    NSMutableArray* dimension2Array = array[i];
    if ([dimension2Array isEqual:[NSNull null]]) {
      dimension2Array = [self arrayWithLength:dimension2Length defaultValue:defaultValue];
    } else {
      dimension2Array =
          [self fixArray:dimension2Array length:dimension2Length defaultValue:defaultValue];
    }
    array[i] = dimension2Array;
  }
  return array;
}

+ (NSMutableArray*)clearAndfixArray:(NSMutableArray*)array
                   dimension1Length:(int)dimension1Length
                   dimension2Length:(int)dimension2Length
                       defaultValue:(NSObject*)defaultValue {
  array = [self clearAndFixArray:array length:dimension1Length defaultValue:[NSNull null]];
  return [self fixArray:array
       dimension1Length:dimension1Length
       dimension2Length:dimension2Length
           defaultValue:defaultValue];
}

#pragma mark -

- (void)removeObjectIfNeed:(id)anObject {
  if (anObject != nil && [self containsObject:anObject]) {
    [self removeObject:anObject];
  }
}

- (void)addObjectIfNeed:(id)anObject {
  if (anObject != nil && ![self containsObject:anObject]) {
    [self addObject:anObject];
  }
}

@end
